package cn.ronghuanet.org.controller;

import cn.ronghuanet.base.exception.BusinessException;
import cn.ronghuanet.org.dto.ShopRegisterDTO;
import cn.ronghuanet.org.service.IShopService;
import cn.ronghuanet.org.domain.Shop;
import cn.ronghuanet.org.query.ShopQuery;
import cn.ronghuanet.base.util.AjaxResult;
import cn.ronghuanet.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PostMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.insert(shop);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {
            Shop shop = shopService.selectById(id);
            return AjaxResult.success(shop);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        try {
            List< Shop> list = shopService.selectAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PutMapping
    public AjaxResult json(@RequestBody ShopQuery query)
    {
        try {
            PageInfo<Shop> pageList = shopService.queryPage(query);
            return AjaxResult.success(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！"+e.getMessage());
        }
    }

    /**
     * 参数接收, employee里面没有confirmPassword
     * 方式一:  在Employee里面加一个confirmPassword字段就可以了
     * 方式二: 单独写一个dto   采纳这种
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@Valid @RequestBody ShopRegisterDTO dto){
        /*try {
            shopService.settlement(dto);
            return AjaxResult.success("店铺入驻成功!");
        }catch (BusinessException e){
            return AjaxResult.error("店铺入驻失败!"+e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("网络异常,请刷新重试!");
        }*/
        shopService.settlement(dto);
        return AjaxResult.success("店铺入驻成功!");
    }

    @GetMapping("/active/{shopId}/{activeCode}")
    public AjaxResult active(@PathVariable("shopId")Long shopId){
        try {
            // 根据店铺ID修改店铺的状态=激活成功
            return AjaxResult.success("sucess!");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("店铺入驻失败!"+e.getMessage());
        }
    }
}
